<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_admin extends CIC_A_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	function index()
	{
		$data	= $this->get_user_admin();
		$this->load->view('admin/user_admin/index',$data);
	}
	
	function get_user_admin()
	{
		$where	= '';
		$and 	= '';
		$url = site_url('admin/user_admin/index?');
		$data['page']	 = $this->page($url, $this->cic_model->get_total_rows('user_admin'), $this->per_page,TRUE);
		$data['user_admin'] = $this->cic_model->get_all('user_admin',$where,array('admin_id','asc'),$this->per_page,$this->input->get('per_page'));
		return $data;
	}
	
	function content_add()
	{
		$data['admin_group'] = $this->cic_model->get_all('admin_group','',array('group_id','asc'));
		$this->load->view('admin/user_admin/content_add',$data);
	}
	
	function content_edit()
	{
		$data = array(
				'user_admin'			=> $this->cic_model->get_one('user_admin',array('admin_id'=>$this->uri->segment(4))),
				'admin_group'			=> $this->cic_model->get_all('admin_group','',array('group_id','asc'))
		);
		$this->load->view('admin/user_admin/content_edit',$data);
	}
	function add_content()
	{
		$this->validate('user_admin', 'admin/user_admin/content_add');
		$data = array(
				'admin_name'			=> $this->input->post('admin_name'),
				'admin_password'		=> md5($this->input->post('admin_password')),
				'group_id'				=> $this->input->post('group_id'),
				'admin_status'			=> $this->input->post('admin_status'),
				'admin_create_time'		=> time(),	
		);
		if($this->cic_model->insert('user_admin',$data))
		{
			$this->message->msg('添加成功！',site_url('admin/user_admin/index'));
		}else{
			$this->message->msg('添加失败！',site_url('admin/user_admin/content_add'));
		}
	}
	
	function edit_content()
	{	
		$data = array(
				'admin_name'			=> $this->input->post('admin_name'),
				'admin_status'			=> $this->input->post('admin_status'),
				'group_id'				=> $this->input->post('group_id'),
		);
		if($this->input->post('admin_password') != '')
		{
			$data['admin_password'] = md5($this->input->post('admin_password'));
		}
		if($this->cic_model->update('user_admin',$data,array('admin_id'=>$this->uri->segment(4))))
		{
			$this->message->msg('修改成功！',site_url('admin/user_admin/index'));
		}else{
			$this->message->msg('未做任何修改！',site_url('admin/user_admin/content_edit/'.$this->uri->segment(4)));
		}
	}
	
	function delete_content()
	{
		if($this->cic_model->delete('user_admin',array('admin_id'=>$this->input->post('admin_id'))))
		{
			echo 1;
		}else{
			echo 0;
		}
	}
	
	function group()
	{
		$data['admin_group'] = $this->cic_model->get_all('admin_group','',array('group_id','asc'));
		$this->load->view('admin/user_admin/group',$data);
	}
	
	function group_add()
	{
		$this->load->view('admin/user_admin/group_add');
	}
	
	function add_group()
	{
		$this->validate('admin_group', 'admin/user_admin/group_add');
		$data = array(
				'group_name'		=> $this->input->post('group_name'),
				'group_desc'		=> $this->input->post('group_desc'),
				'group_privilege'	=> serialize($this->input->post('group_privilege')), 
		);
		if($this->cic_model->insert('admin_group',$data))
		{
			$this->message->msg('分组添加成功！',site_url('admin/user_admin/group'));
		}else{
			$this->message->msg('分组添加失败！',site_url('admin/user_admin/group_add'));
		}
	}
	
	function group_edit()
	{
		$data['group'] = $this->cic_model->get_one('admin_group',array('group_id'=>$this->uri->segment(4)));
		$this->load->view('admin/user_admin/group_edit',$data);
	}
	
	function edit_group()
	{
		$this->validate('admin_group', 'admin/user_admin/group_edit/'.$this->input->post('group_id'));
		$data = array(
				'group_name'		=> $this->input->post('group_name'),
				'group_desc'		=> $this->input->post('group_desc'),
				'group_privilege'	=> serialize($this->input->post('group_privilege')),
		);
		if($this->cic_model->update('admin_group',$data,array('group_id'=>$this->input->post('group_id'))))
		{
			$this->message->msg('修改成功！',site_url('admin/user_admin/group'));
		}else{
			$this->message->msg('未做任何修改！',site_url('admin/user_admin/group_edit/'.$this->input->post('group_id')));
		}
	}
	
	function delete_group()
	{
		echo $this->cic_model->delete('admin_group',array('group_id'=>$this->input->post('group_id')));	
	}
}